<template>
  <div class="page">
    <el-form :model="searchForm" label-position="left" inline label-suffix=":" size="small">
      <div class="flex wap flex-start">
        <el-form-item label="学生">
          <check-stu :itemId="searchForm.stuid" @checkedItem="checkedStu"/>
        </el-form-item>
        <el-form-item label="课程">
          <check-lesson :itemId="searchForm.lessionid" @checkedItem="checkedlession"></check-lesson>
        </el-form-item>
        <el-form-item label="是否通过">
          <el-select v-model="searchForm.ispass" placeholder="请选择" clearable>
            <el-option label="待考核" :value="0"></el-option>
            <el-option label="通过" :value="1"></el-option>
            <el-option label="未通过" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否缴费">
          <el-select v-model="searchForm.ispay" placeholder="请选择" clearable>
            <el-option label="未缴费" :value="0"></el-option>
            <el-option label="已缴费" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="getData">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
    <div class="flex wap flex-start" style="padding: 0px 0 10px 0">
      <el-button type="success" @click="addBtn">新增</el-button>
      <div style="width:20px"></div>
    </div>
    <el-table :data="tableData" border style="width: 100%"  height="600"  :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        'text-align': 'center'
      }"
      :cell-style="{ 'text-align': 'center' }">
      <el-table-column type="selection"></el-table-column>
      <!-- <el-table-column prop="kaojiid" label="考级ID"></el-table-column> -->
      <el-table-column prop="stuname" label="学员名字"></el-table-column>
      <el-table-column prop="stuphone" label="手机号"></el-table-column>
      <el-table-column prop="stuidcard" label="身份证"></el-table-column>
      <el-table-column prop="lessionname" label="课程"></el-table-column>
      <el-table-column prop="ispay" label="是否付款">
        <template slot-scope="scop">
          <el-tag type="success" v-if="scop.row.ispay == 0">未付款</el-tag>
          <el-tag type="warning" v-if="scop.row.ispay == 1">已付款</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="payinfo" label="付款信息" show-overflow-tooltip></el-table-column>
      <el-table-column prop="price" label="费用"></el-table-column>
      <el-table-column prop="level" label="等级"></el-table-column>
      <el-table-column prop="ispass" label="是否通过">
        <template slot-scope="scop">
          <el-tag type="danger" v-if="scop.row.ispass == 0">待考核</el-tag>
          <el-tag type="success" v-if="scop.row.ispass == 1">通过</el-tag>
          <el-tag type="warning" v-if="scop.row.ispass == 2">未通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createtime" label="创建时间" show-overflow-tooltip></el-table-column>
      <el-table-column prop="" label="操作" width="120">
        <template slot-scope="scop">
          <el-button type="primary" icon="el-icon-edit" circle @click="updateBtn(scop.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="del(scop.row.kaojiid)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="float:right;padding:10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog title="新增/修改考级信息" center :visible.sync="dialogFormVisible" width="40%">
      <el-form :model="addOrEditForm" ref="RefAddOrEditForm" :rules="rule" label-width="100px" class="edit-form"
        label-suffix="：">
        <el-form-item label="课程" prop="lessionid" v-if="addOrEditForm.kaojiid==''">
          <check-lesson :itemId="addOrEditForm.lessionid" @checkedItem="checkedlession1"></check-lesson>
        </el-form-item>
        <el-form-item label="学员" prop="stuid" v-if="addOrEditForm.kaojiid==''">
          <check-stu :itemId="addOrEditForm.stuid" @checkedItem="checkedStu1"/>
        </el-form-item>
        <el-form-item label="付款信息" prop="payinfo">
          <el-input type="text" placeholder="请输入付款信息" v-model="addOrEditForm.payinfo"></el-input>
        </el-form-item>
        <el-form-item label="是否付款" prop="ispay">
          <el-radio-group v-model="addOrEditForm.ispay">
            <el-radio :label="0">未付款</el-radio>
            <el-radio :label="1">已付款</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="费用" prop="price">
          <el-input type="text" placeholder="请输入费用" v-model="addOrEditForm.price"></el-input>
        </el-form-item>
        <el-form-item label="等级" prop="level">
          <el-input type="text" placeholder="请输入费用" v-model="addOrEditForm.level"></el-input>
        </el-form-item>
        <el-form-item label="是否通过" prop="ispass">
          <el-radio-group v-model="addOrEditForm.ispass">
            <el-radio :label="0">待考核</el-radio>
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">未通过</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { findKaojiList, addKaoji, updateKaoji, delKaoji } from "@/api/user";
import checkStu from "@/views/components/checkStu";
import checkLesson from "@/views/components/checkLesson"
export default {
  components: {
    checkStu,
    checkLesson
  },
  data() {
    return {
      total: 0,
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      dialogFormVisible: false,
      searchForm: {
        lessionid: "",
        stuid: "",
        ispass: "",
        ispay: "",
      },
      addOrEditForm: {
        kaojiid: '',
        lessionid: '',
        stuid: '',
        payinfo: '',
        ispay: '',
        price: '',
        level: '',
        ispass: ''
      },
      rule: {
        lessionid: [
          { required: true, message: '请选择课程', trigger: 'change' }
        ],
        stuid: [
          { required: true, message: '请选择学员', trigger: 'change' }
        ]
      }
    };
  },
  computed: {
    queryData() {
      const { lessionid, stuid, ispass, ispay } = this.searchForm
      return {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        lessionid,
        stuid,
        ispass,
        ispay
      };
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      const res = await findKaojiList.post(this.queryData);
      if (res.resultCode === "000000") {
        this.tableData = res.data.kaojiList;
        this.total = res.data.count;
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    addBtn() {
      this.dialogFormVisible = true
      this.addOrEditForm = {
        kaojiid: '',
        lessionid: '',
        stuid: '',
        payinfo: '',
        ispay: '',
        price: '',
        level: '',
        ispass: ''
      }
    },
    updateBtn(val) {
      this.dialogFormVisible = true
      const { kaojiid, lessionid, stuid, payinfo, ispay, price, level, ispass } = val
      this.addOrEditForm = {
        kaojiid,
        lessionid,
        stuid,
        payinfo,
        ispay,
        price,
        level,
        ispass
      }
    },
    save() {
      this.$refs.RefAddOrEditForm.validate(async (valid) => {
        if (valid) {
          if (this.addOrEditForm.kaojiid === '') {
            const res = await addKaoji.post(this.addOrEditForm);
            if (res.resultCode === "000000") {
              this.$message.success(res.resultInfo);
              this.dialogFormVisible = false
              this.getData()
            } else {
              this.$message.error(res.resultInfo);
            }
          } else {
            const res = await updateKaoji.post(this.addOrEditForm);
            if (res.resultCode === "000000") {
              this.$message.success(res.resultInfo);
              this.dialogFormVisible = false
              this.getData()
            } else {
              this.$message.error(res.resultInfo);
            }
          }
        } else {
          return false;
        }
      });
    },
    checkedStu(val) {
      this.searchForm.stuid = val
    },
    checkedlession(val) {
      this.searchForm.lessionid = val
    },
    checkedStu1(val) {
      this.addOrEditForm.stuid = val
    },
    checkedlession1(val) {
      this.addOrEditForm.lessionid = val
    },
    del(id) {
      this.$confirm("此操作将删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.delItem(id)
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async delItem(id) {
      const res = await delKaoji.post({ kaojiid: id });
      if (res.resultCode === "000000") {
        this.$message.success(res.resultInfo);
        this.getData()
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getData();
    }
  }
};
</script>
<style lang="less" scoped></style>
